package com.Socket.Server;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private int port = 6666;//通过端口监听客户端的连接
    private ServerSocket serverSocket = null;
    private Socket s =null;
    private void monitor(){
        if(serverSocket!=null){
            while(true) {
                try {
                    s= serverSocket.accept();
                    System.out.println("連接成功");
                    InputStream is = s.getInputStream();
                    Reader isr = new InputStreamReader(is);
                    BufferedReader reader = new BufferedReader(isr);
                    String s = null;
                    Writer osw = new OutputStreamWriter(new FileOutputStream("xml/Result1.xml"));
                    BufferedWriter writer = new BufferedWriter(osw);

                    while(true){
                        s= reader.readLine();
                        writer.write(s);
                        writer.newLine();
                        writer.flush();
                        if(s.equals("</Users>")){
                            break;
                        }
                    }
                    writer.flush();
                    writer.close();
                    String result =null;
                    if(JudgeOfTable.Judge(new AnalysisXML().insertInSQL())) {
                         result = "登录成功！";
                    }else{
                        result = "登录失败！";
                    }
                    DataOutputStream dos =new DataOutputStream(this.s.getOutputStream());
                    dos.writeUTF(result);
                    dos.close();
                    reader.close();
                    dos.close();
                    this.s.close();
                } catch (IOException e) {
                    System.out.println("连接不被允许!");
                    e.printStackTrace();
                }
            }
        }
    }
    public  void  createServer (){
        try {
                serverSocket = new ServerSocket(port);//监听这个端口
                monitor();
        }catch (IOException e){
            System.out.println(""+port+":端口占用");
            e.printStackTrace();
        }
    }
}
